// test hihi
package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import view.PanelSystem;

public class AccountDataController implements ActionListener, FocusListener,
		KeyListener {
	private PanelSystem panel;
	private Connection connect;
	private Statement statement;
	private ResultSet rs;
	private PreparedStatement preStm;

	public AccountDataController(PanelSystem panel) {
		this.panel = panel;
	}

	// TODO action event
	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		switch (cmd) {
		case "add":
			addAccount();
			break;
		case "del":// used by delete menu
			if (panel.getQueryDel().equalsIgnoreCase("admin")) {
				JOptionPane.showMessageDialog(panel,
						"Đây là tài khoản hệ thống không thể bị xóa",
						"Cảnh báo", JOptionPane.WARNING_MESSAGE);
				return;
			} else if (JOptionPane.showConfirmDialog(panel,
					"Xác nhận xóa tài khoản " + panel.getQueryDel(),
					"Cảnh báo", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)
				delAccount();
			break;
		case "exit": // used by cancel button
			panel.exit();
			break;
		case "user":
			panel.checkUserName();
			break;
		case "author":
			panel.checkAuthor();
			break;
		case "pass":
			panel.checkPass();
			break;
		case "repass":
			if (panel.checkReenter())
				panel.getBtnAdd().doClick();
			break;
		}
	}

	@Override
	public void focusGained(FocusEvent e) {
		((JTextField) e.getSource()).selectAll();
	}

	@Override
	public void focusLost(FocusEvent e) {
		String tmp = ((JTextField) e.getSource()).getName();
		if (tmp.isEmpty())
			return;
		switch (tmp) {
		case "user":
			panel.checkUserName();
			break;
		case "author":
			panel.checkAuthor();
			break;
		case "pass":
			panel.checkPass();
			break;
		case "repass":
			panel.checkReenter();
			break;
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {

	}

	@Override
	public void keyPressed(KeyEvent e) {
	}

	@Override
	public void keyReleased(KeyEvent e) {

	}

	// TODO add account to system
	public void addAccount() {
		if (panel.checkUserName() && panel.checkPass() && panel.checkReenter()) {
			try {
				connect = model.Database.connect();

				PreparedStatement preStm = connect
						.prepareStatement("INSERT INTO account(clock,name,author,password,exe) VALUES(?,?,?,?,?)");
				String[] query = panel.getQueryAdd();
				for (int i = 0; i < query.length; i++) {
					preStm.setString(i + 1, query[i]);
				}

				preStm.execute();
				loadData(panel.getData());
				panel.clearAddPanel();
				JOptionPane.showMessageDialog(panel,
						"Thêm tài khoản thành công", null,
						JOptionPane.INFORMATION_MESSAGE);
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			JOptionPane
					.showMessageDialog(
							panel,
							"Thông tin tài khoản hoặc mật khẩu không hợp lệ\nVui lòng xem lại các ràng buộc!",
							"Lỗi", JOptionPane.ERROR_MESSAGE);
		}
	}

	// TODO remove account from system
	public void delAccount() {
		try {
			connect = model.Database.connect();

			preStm = connect
					.prepareStatement("DELETE FROM account WHERE name = ?");
			preStm.setString(1, panel.getQueryDel());

			preStm.execute();
			loadData(panel.getData());
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	// TODO load data from database and show in table
	public void loadData(Vector<Vector<Object>> data) {
		data.clear();
		try {
			connect = model.Database.connect();

			statement = connect.createStatement();
			String sql = "select * from account";
			rs = statement.executeQuery(sql);

			int c = 1;
			while (rs.next()) {
				Vector<Object> tmp = new Vector<Object>();
				tmp.add(c + "");
				for (int i = 1; i < 7; i++) {
					if (i == 1)
						tmp.add(rs.getBoolean(i));
					else
						tmp.add(rs.getString(i));
				}
				c++;
				data.add(tmp);
			}

			panel.setTableModel(data);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				statement.close();
				connect.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
